import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Worker from '../components/worker/Workers.vue'
import Supplier from '../components/supplier/Supplier.vue'
import Goods from '../components/goods/Goods.vue'
import PurchaseMaster from '../components/purchasemaster/PurchaseMaster.vue'
import Power from '../components/power/Power.vue'
import Roles from '../components/roles/Roles.vue'
import PurchaseDetail from '../components/purchaseDetails/PurchaseDetails.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/worker', component: Worker },
      { path: '/supplier', component: Supplier },
      { path: '/goods', component: Goods },
      { path: '/purchase_master', component: PurchaseMaster },
      { path: '/power', component: Power },
      { path: '/roles', component: Roles },
      { path: '/purchase_details', component: PurchaseDetail }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to 将要访问路径
  // from 代表从哪跳转
  // next 是一个函数代表放行
  if (to.path === '/login') return next()
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
